//
//  FilterPopView.m
//  LvXi_newVersion
//
//  Created by 王敏 on 16/8/31.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "FilterPopView.h"
@interface FilterPopView(){
    BOOL _isHidden;
}


@property(nonatomic,strong)NSMutableArray * priceRaneButtonArray;

@property(nonatomic,strong)UIButton * selectedPriceRangeButton;

@property(nonatomic,strong)NSMutableArray * discountButtonArray;

@property(nonatomic,strong)NSMutableArray * selectedDiscountButtonArray;
@end
@implementation FilterPopView


-(id)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        [self initArrays];
        self.backgroundColor = [UIColor whiteColor];
        
        
        UILabel * pricelabel = [[UILabel alloc] initWithFrame:CGRectMake(globalMargin, globalMargin, self.width-2*globalMargin, Category_height(15))];
        pricelabel.font = [UIFont systemFontOfSize:12];
        pricelabel.text = @"价格";
        [self addSubview:pricelabel];
        CGFloat space = (self.width-4*Category_width(76))/6.0;
        NSArray *priceRanes = @[@"100以下",@"100-500",@"500-1000",@"1000以上"];
        [priceRanes enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            UIButton * button = [self roundRectBurronWith:obj target:self action:@selector(optionButtonClick:)];
            button.x = idx*(space+button.width)+space;
            button.y = pricelabel.botton+globalMargin;
            button.tag = idx;
            [self.priceRaneButtonArray addObject:button];
            [self addSubview:button];
        }];
        UIButton * prButton = self.priceRaneButtonArray.lastObject;
        UILabel * discountLabel = [[UILabel alloc] initWithFrame:CGRectMake(globalMargin, prButton.botton+globalMargin,pricelabel.width, Category_height(15))];
        discountLabel.font = [UIFont systemFontOfSize:12];
        discountLabel.text = @"折扣和服务";
        [self addSubview:discountLabel];
        
        
        NSArray *discounts = @[@"包邮",@"消费者保障",@"全球购",@"货到付款",@"分期付款",@"绿豆抵现",@"7天内退货",@"通用排序",];
        [discounts enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            UIButton * button = [self roundRectBurronWith:obj target:self action:@selector(optionButtonClick:)];
            button.x = (idx%4)*(space+button.width)+space;
            button.y = discountLabel.botton+globalMargin+((idx/4)*(button.height+globalMargin));
            button.tag = idx+4;
            [self.discountButtonArray addObject:button];
            [self addSubview:button];
        }];

        
        UIButton * bbb = self.discountButtonArray.lastObject;
        UIButton * resetButton = [self roundRectBurronWith:@"重置" target:self action:@selector(optionButtonClick:)];
        resetButton.centerX = self.width/4;
        resetButton.y = bbb.botton+2*globalMargin;
        resetButton.tag = 100;
        [self addSubview:resetButton];
        
        UIButton * okButton = [self roundRectBurronWith:@"确定" target:self action:@selector(optionButtonClick:)];
        okButton.centerX = self.width/4*3;
        [okButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        okButton.y = bbb.botton+2*globalMargin;
        okButton.tag = 200;
        okButton.backgroundColor = HexColor(@"#3CB034");
        [self addSubview:okButton];
        
        self.height = okButton.botton + 2*globalMargin;
        
    }
    return self;
}

-(void)initArrays{
    self.priceRaneButtonArray = [NSMutableArray array];
    self.discountButtonArray = [NSMutableArray array];
    self.selectedDiscountButtonArray = [NSMutableArray array];
}

-(void)optionButtonClick:(UIButton*)button{
    switch (button.tag) {
        case 0:case 1:case 2:
        case 3:{
            if(self.selectedPriceRangeButton){
                self.selectedPriceRangeButton.selected = NO;
                self.selectedPriceRangeButton.backgroundColor = HexColor(@"#FFFFFF");
            }
            button.selected = !button.selected;
            if(button.selected){
                button.backgroundColor = HexColor(@"#3CB034");
            }
            else{
                button.backgroundColor = HexColor(@"#FFFFFF");
            }
            self.selectedPriceRangeButton = button;

        }break;
        case 4:case 5:case 6:case 7:case 8:case 9:case 10:
        case 11:{
            if([self.selectedDiscountButtonArray indexOfObject:button] == NSNotFound){
                button.selected = YES;
                button.backgroundColor = HexColor(@"#3CB034");
                [self.selectedDiscountButtonArray addObject:button];
            }else{
                button.selected = NO;
                button.backgroundColor = HexColor(@"#FFFFFF");
                [self.selectedDiscountButtonArray removeObject:button];
            }
 
        }break;
        case 100:{
            self.selectedPriceRangeButton.selected = NO;
             self.selectedPriceRangeButton.backgroundColor = HexColor(@"#FFFFFF");
            [self.selectedDiscountButtonArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                UIButton * btn = obj;
                btn.selected = NO;
                btn.backgroundColor = HexColor(@"#FFFFFF");
            }];
            [self.selectedDiscountButtonArray removeAllObjects];
        }break;
        case 200:{
            if([self.delegate respondsToSelector:@selector(FilterPopView:didSelectedWithPriceRangeType:discounts:)]){
                [self.delegate FilterPopView:self
               didSelectedWithPriceRangeType:self.selectedPriceRangeButton?self.selectedPriceRangeButton.tag:
                 0 discounts:self.selectedDiscountButtonArray];
            }
        }break;
        default:
            break;
    }
}

-(UIButton*)roundRectBurronWith:(NSString*)title target:(id)target action:(SEL)action{
    
    CGSize size = CGSizeMake(Category_width(76), Category_height(26));
    UIButton * button = [[UIButton alloc] init];
    button.size = size;
    [button addRadius:5];
    button.layer.borderWidth = 0.5;
    button.layer.borderColor = HexColor(@"#666666").CGColor;
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:HexColor(@"#333333") forState:UIControlStateNormal];
    [button setTitleColor:HexColor(@"#FFFFFF") forState:UIControlStateSelected];
    button.titleLabel.font = [UIFont systemFontOfSize:12];

    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    button.adjustsImageWhenHighlighted = NO;
    button.highlighted = NO;
    return button;
    
}

@end
